package com.example.design.c.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 中介者模式
 */
public class MediatorPattern {
    public static void main(String[] args) {
        Mediator md = new ConcreteMediator();
        Colleague c1, c2;
        c1 = new ConcreteColleague1();
        c2 = new ConcreteColleague2();
        md.register(c1);
        md.register(c2);
        c1.send();
        System.out.println("-------------");
        c2.send();
    }
}

//抽象中介者
abstract class Mediator {

    public abstract void register(Colleague colleague);

    public abstract void relay (Colleague cl);//转发

}

//具体中介者
class ConcreteMediator extends Mediator{

    private List<Colleague> colleagueList =new ArrayList<Colleague>();

    @Override
    public void register(Colleague colleague) {

        if(!colleagueList.contains(colleague)){
            colleagueList.add(colleague);
            colleague.setMedium(this);
        }
    }

    @Override
    public void relay(Colleague cl) {
        for (Colleague ob:colleagueList){
            if(!ob.equals(cl)){
                ((Colleague)ob).receive();
            }
        }
    }
}

//抽象同事类
abstract class Colleague {

    protected Mediator mediator;

    public void setMedium(Mediator mediator){
        this.mediator = mediator;
    }

    public abstract void receive();

    public abstract void send();

}

//具体同事类1
class ConcreteColleague1 extends Colleague {

    @Override
    public void receive() {
        System.out.println("具体同事类1收到请求。");
    }

    @Override
    public void send() {
        System.out.println("具体同事类1发出请求。");
        mediator.relay(this);
    }
}

//具体同事类2
class ConcreteColleague2 extends Colleague {

    @Override
    public void receive() {
        System.out.println("具体同事类2收到请求。");
    }

    @Override
    public void send() {
        System.out.println("具体同事类2发出请求。");
        mediator.relay(this);
    }
}